package net.caixiaomi.info.adapter;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;

import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.MatchAsiaItem;
import net.caixiaomi.info.model.MatchDXItem;
import net.caixiaomi.info.model.MatchEuropeItem;
import net.caixiaomi.info.util.Constants;

import java.util.List;

public class MatchDetailOddsAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MatchDetailOddsAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(Constants.ITEM_VIEW_ASIA, net.caixiaomi.info.R.layout.item_analysis_odds);
        addItemType(Constants.ITEM_VIEW_EUROPE, net.caixiaomi.info.R.layout.item_analysis_odds);
        addItemType(Constants.ITEM_VIEW_DAXIAO, net.caixiaomi.info.R.layout.item_analysis_odds);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        try {
            switch (item.getItemType()) {
                case Constants.ITEM_VIEW_ASIA:
                    bindAsiaView(helper, item);
                    break;
                case Constants.ITEM_VIEW_EUROPE:
                    bindEuropeView(helper, item);
                    break;
                case Constants.ITEM_VIEW_DAXIAO:
                    bindDXView(helper, item);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindDXView(BaseViewHolder helper, MultiItemEntity item) {
        try {
            try {
                StringBuilder stringBuilder = new StringBuilder();

                MatchDXItem model = (MatchDXItem) item;
                helper.setTextColor(net.caixiaomi.info.R.id.company, ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
                helper.setText(net.caixiaomi.info.R.id.company, model.getComName());
                helper.setVisible(net.caixiaomi.info.R.id.tip, true);


                int change = Integer.valueOf(model.getWinChange());

                stringBuilder.append(model.getInitWin()).append("\n").append(model.getRealWin());
                if (change == 1) {
                    stringBuilder.append("↑");
                } else if (change == 2) {
                    stringBuilder.append("↓");
                }
                SpannableString spannableString = new SpannableString(stringBuilder);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), change == 1 ? net.caixiaomi.info.R.color.orange_primary : change == 2 ? net.caixiaomi.info.R.color.green : net.caixiaomi.info.R.color.primary_text)), model.getInitWin().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                helper.setText(net.caixiaomi.info.R.id.win, spannableString);
                helper.setTextColor(net.caixiaomi.info.R.id.win,ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
                stringBuilder.setLength(0);
                change = Integer.valueOf(model.getDrawChange());
                stringBuilder.append(model.getInitDraw()).append("\n").append(model.getRealDraw());
                if (change == 1) {
                    stringBuilder.append("↑");
                } else if (change == 2) {
                    stringBuilder.append("↓");
                }
                spannableString = new SpannableString(stringBuilder);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), change == 1 ? net.caixiaomi.info.R.color.orange_primary : change == 2 ? net.caixiaomi.info.R.color.green : net.caixiaomi.info.R.color.primary_text)), model.getInitDraw().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                helper.setText(net.caixiaomi.info.R.id.draw, spannableString);
                helper.setTextColor(net.caixiaomi.info.R.id.draw,ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
                stringBuilder.setLength(0);
                change = Integer.valueOf(model.getLoseChange());
                stringBuilder.append(model.getInitLose()).append("\n").append(model.getRealLose());
                if (change == 1) {
                    stringBuilder.append("↑");
                } else if (change == 2) {
                    stringBuilder.append("↓");
                }
                spannableString = new SpannableString(stringBuilder);
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), change == 1 ? net.caixiaomi.info.R.color.orange_primary : change == 2 ? net.caixiaomi.info.R.color.green : net.caixiaomi.info.R.color.primary_text)), model.getInitLose().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                helper.setText(net.caixiaomi.info.R.id.defeat, spannableString);
                helper.setTextColor(net.caixiaomi.info.R.id.defeat,ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
            } catch (Exception e) {
                e.printStackTrace();
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindEuropeView(BaseViewHolder helper, MultiItemEntity item) {
        try {
            StringBuilder stringBuilder = new StringBuilder();

            MatchEuropeItem model = (MatchEuropeItem) item;
            helper.setTextColor(net.caixiaomi.info.R.id.company, ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
            helper.setText(net.caixiaomi.info.R.id.company, model.getComName());
            helper.setVisible(net.caixiaomi.info.R.id.tip, true);


            int change = Integer.valueOf(model.getWinChange());

            stringBuilder.append(model.getInitWin()).append("\n").append(model.getRealWin());
            if (change == 1) {
                stringBuilder.append("↑");
            } else if (change == 2) {
                stringBuilder.append("↓");
            }
            SpannableString spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), change == 1 ? net.caixiaomi.info.R.color.orange_primary : change == 2 ? net.caixiaomi.info.R.color.green : net.caixiaomi.info.R.color.primary_text)), model.getInitWin().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.setText(net.caixiaomi.info.R.id.win, spannableString);
            helper.setTextColor(net.caixiaomi.info.R.id.win,ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
            stringBuilder.setLength(0);
            change = Integer.valueOf(model.getDrawChange());
            stringBuilder.append(model.getInitDraw()).append("\n").append(model.getRealDraw());
            if (change == 1) {
                stringBuilder.append("↑");
            } else if (change == 2) {
                stringBuilder.append("↓");
            }
            spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), change == 1 ? net.caixiaomi.info.R.color.orange_primary : change == 2 ? net.caixiaomi.info.R.color.green : net.caixiaomi.info.R.color.primary_text)), model.getInitDraw().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.setText(net.caixiaomi.info.R.id.draw, spannableString);
            helper.setTextColor(net.caixiaomi.info.R.id.draw,ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
            stringBuilder.setLength(0);
            change = Integer.valueOf(model.getLoseChange());
            stringBuilder.append(model.getInitLose()).append("\n").append(model.getRealLose());
            if (change == 1) {
                stringBuilder.append("↑");
            } else if (change == 2) {
                stringBuilder.append("↓");
            }
            spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), change == 1 ? net.caixiaomi.info.R.color.orange_primary : change == 2 ? net.caixiaomi.info.R.color.green : net.caixiaomi.info.R.color.primary_text)), model.getInitLose().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.setText(net.caixiaomi.info.R.id.defeat, spannableString);
            helper.setTextColor(net.caixiaomi.info.R.id.defeat,ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindAsiaView(BaseViewHolder helper, MultiItemEntity item) {
        try {
            StringBuilder stringBuilder = new StringBuilder();

            MatchAsiaItem model = (MatchAsiaItem) item;
            helper.setTextColor(net.caixiaomi.info.R.id.company, ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
            helper.setText(net.caixiaomi.info.R.id.company, model.getComName());
            helper.setVisible(net.caixiaomi.info.R.id.tip, true);
            int oddsChange = Integer.valueOf(model.getOdds1Change());
            stringBuilder.append(model.getInitOdds1()).append("\n").append(model.getRealOdds1());
            if (oddsChange == 1) {
                stringBuilder.append("↑");
            } else if (oddsChange == 2) {
                stringBuilder.append("↓");
            }
            SpannableString spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), oddsChange == 1 ? net.caixiaomi.info.R.color.orange_primary : oddsChange == 2 ? net.caixiaomi.info.R.color.green : net.caixiaomi.info.R.color.primary_text)), model.getInitOdds1().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.setText(net.caixiaomi.info.R.id.win, spannableString);
            helper.setTextColor(net.caixiaomi.info.R.id.win,ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
            stringBuilder.setLength(0);
            stringBuilder.append(model.getInitRule()).append("\n").append(model.getRealRule());
            helper.setText(net.caixiaomi.info.R.id.draw, stringBuilder);
            helper.setTextColor(net.caixiaomi.info.R.id.draw,ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
            oddsChange = Integer.valueOf(model.getOdds2Change());
            stringBuilder.setLength(0);
            stringBuilder.append(model.getInitOdds2()).append("\n").append(model.getRealOdds2());
            if (oddsChange == 1) {
                stringBuilder.append("↑");
            } else if (oddsChange == 2) {
                stringBuilder.append("↓");
            }
            spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), oddsChange == 1 ? net.caixiaomi.info.R.color.orange_primary : oddsChange == 2 ? net.caixiaomi.info.R.color.green : net.caixiaomi.info.R.color.primary_text)), model.getInitOdds2().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.setText(net.caixiaomi.info.R.id.defeat, spannableString);
            helper.setTextColor(net.caixiaomi.info.R.id.defeat,ContextCompat.getColor(CommonApp.getInstance(), net.caixiaomi.info.R.color.primary_text));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
